//
//  ShoppingViewController.m
//  FunSport_WWL
//
//  Created by 王黎 on 16/4/28.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "ShoppingViewController.h"
#import "ShoppingModel.h"
#import "ShoppingCell.h"
#import "ShoppingDetailViewController.h"
#import "RootViewController.h"


@interface ShoppingViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,retain)NSMutableArray *dataSourceArray;//存储model中的数据

@property (nonatomic,retain)NSMutableDictionary *parameter;//参数

@property (nonatomic,assign)NSInteger page;


@end

@implementation ShoppingViewController


#pragma mark --- 懒加载

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight)];
        
        //设置代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
}

- (NSMutableArray *)dataSourceArray {
    
    if (!_dataSourceArray) {
        
        _dataSourceArray = [NSMutableArray array];
        
    }
    
    return _dataSourceArray;
    
}

- (NSMutableDictionary *)parameter {

    if (!_parameter) {

        _parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"page":@1}];

    }

    return _parameter;
}

#pragma mark --- 网络请求数据

- (void)fetchDataSourceWithUrl:(NSString *)url paramete:(NSMutableDictionary *)parameter  {
    
    //添加菊花图
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        //JSON解析数据
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingAllowFragments) error:nil];
        NSLog(@"%@",dic);
        NSArray *array = [dic objectForKey:@"data"];
        
        //遍历数组取值
        for (NSDictionary *object in array) {
            
            //KVC赋值
            ShoppingModel *model = [[ShoppingModel alloc] init];
            
            [model setValuesForKeysWithDictionary:object];
            
            [self.dataSourceArray addObject:model];
            
            NSLog(@"model ---  %@",model);
            
        }
        
        
        //移除菊花图
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_footer endRefreshing];
        //移除菊花图
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
    }];
    
}


#pragma mark --- tableView代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShoppingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shop_cell" forIndexPath:indexPath];
    
    [cell setCellWithShoppingModel:self.dataSourceArray[indexPath.row]];
    
    return cell;
    
}


//点击单元格进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShoppingModel *model = self.dataSourceArray[indexPath.row];
    
    ShoppingDetailViewController *detailVC = [[ShoppingDetailViewController alloc] init];
    
    detailVC.article_id = model.article_id;
    
    [self presentViewController:detailVC animated:YES completion:nil];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加
    [self.view addSubview:self.tableView];
    
    //设置行高
    self.tableView.rowHeight = 100;
    
    
    //注册xib
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ShoppingCell" bundle:nil] forCellReuseIdentifier:@"shop_cell"];
    
    
    //调用请求数据的方法
    //[self fetchDataSourceWithUrl:kShoppingUrl parameter:self.parameter];
    [self fetchDataSourceWithUrl:kShoppingUrl paramete:self.parameter];
    
    self.title = @"体育装备";
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self.parameter setValue:[NSNumber numberWithInteger:self.page] forKey:@"page"];
        
        [self fetchDataSourceWithUrl:kShoppingUrl paramete:self.parameter];
        
    }];
    
   // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.dataSourceArray = nil;
        
        [self fetchDataSourceWithUrl:kShoppingUrl paramete:self.parameter];
        
        
       
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
